//
//  SACommonProblemsDetailVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/1/5.
//

#import "SACommonProblemsDetailVC.h"
#import "SASystemFaqQuestionListAPI.h"
#import "SACommonProblemsCell.h"
#import "SACommonProblemsModel.h"
#import "SACommonProblemsDetailModel.h"
#import "HDWKWebVC.h"

@interface SACommonProblemsDetailVC ()<UITextFieldDelegate,HTTPAPIDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SASystemFaqQuestionListAPI *systemFaqQuestionListAPI;
@property (nonatomic, strong) SACommonProblemsModel *model;
@property (nonatomic, strong) NSMutableArray<SACommonProblemsDetailModel *> *dataMarr;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation SACommonProblemsDetailVC

-(instancetype)initWithProblemsModel:(SACommonProblemsModel *)model {
    if (self = [super init]) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.systemFaqQuestionListAPI == api) {
        self.dataMarr = [SACommonProblemsDetailModel mj_objectArrayWithKeyValuesArray:data[@"data"][@"records"]];
        [self.tableView reloadData];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SACommonProblemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACommonProblemsCell" forIndexPath:indexPath];
    cell.detailModel = self.dataMarr[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SACommonProblemsDetailModel *model = self.dataMarr[indexPath.section];
    NSString *basepath = kServerHost;
    NSString *serverPort = kServerPort;
    if (serverPort.length > 0) {
        basepath = [NSString stringWithFormat:@"%@:%@",kServerHost,kServerPort];
    }
    NSString *url = [NSString stringWithFormat:@"%@/system/faq/question/answer?id=%@",basepath,model.id];
    HDWKWebVC *pushVC = [[HDWKWebVC alloc] initWithTitle:model.question AndUrl:url];

    [self.navigationController pushViewController:pushVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(1);
}

#pragma mark - textField
- (void)textFieldEditChanged:(UITextField *)textField {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchProduct2:) object:textField];
    [self performSelector:@selector(searchProduct2:) withObject:textField afterDelay:0.3];
}

- (void)searchProduct2:(UITextField *)textField {
    self.currentPage = 1;
    self.systemFaqQuestionListAPI.keyword = textField.text;
    [self loadData];
}

-(void)loadData {
    self.systemFaqQuestionListAPI.currentPage = self.currentPage;
    [self.systemFaqQuestionListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:self.model.typeDesc];
    [self setBgImage:nil];
    self.view.backgroundColor = kLineColor;
    
    [self.view addSubview:self.whiteView];
    [self.view addSubview:self.searchTf];
    [self.view addSubview:self.tableView];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
    
    self.currentPage = 1;
    self.systemFaqQuestionListAPI.pageSize = @"20";
    self.systemFaqQuestionListAPI.currentPage = self.currentPage;
    self.systemFaqQuestionListAPI.questionType = self.model.id;
    [self.systemFaqQuestionListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 60)];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView;
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(30, StatusHeight + 10, 315, 40)];
        _searchTf.placeholder = @"输入问题";
        _searchTf.backgroundColor = kLineColor;
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 40)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 12, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        _searchTf.returnKeyType = UIReturnKeyDone;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTf;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(65 + StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(65 + StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kLineColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SACommonProblemsCell class] forCellReuseIdentifier:@"SACommonProblemsCell"];
    }
    return _tableView;
}

-(SASystemFaqQuestionListAPI *)systemFaqQuestionListAPI {
    if (!_systemFaqQuestionListAPI) {
        _systemFaqQuestionListAPI = [SASystemFaqQuestionListAPI apiWithDelegate:self];
    }
    return _systemFaqQuestionListAPI;
}

-(NSMutableArray<SACommonProblemsDetailModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

@end
